package com.djf.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 用户配送实体类
 * 用户配送表按月分表，与订单表保持一致的分表策略
 *
 * @author djf
 * @since 2025-01-01
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("user_delivery")
public class UserDelivery implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 配送记录ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 用户ID
     */
    @TableField("user_id")
    private Long userId;

    /**
     * 订单ID
     */
    @TableField("order_id")
    private Long orderId;

    /**
     * 配送单号
     */
    @TableField("delivery_no")
    private String deliveryNo;

    /**
     * 收货人姓名
     */
    @TableField("receiver_name")
    private String receiverName;

    /**
     * 收货人电话
     */
    @TableField("receiver_phone")
    private String receiverPhone;

    /**
     * 配送地址
     */
    @TableField("delivery_address")
    private String deliveryAddress;

    /**
     * 省份
     */
    @TableField("province")
    private String province;

    /**
     * 城市
     */
    @TableField("city")
    private String city;

    /**
     * 区县
     */
    @TableField("district")
    private String district;

    /**
     * 配送状态：1-待配送，2-配送中，3-已送达，4-配送失败
     */
    @TableField("delivery_status")
    private Integer deliveryStatus;

    /**
     * 快递员姓名
     */
    @TableField("courier_name")
    private String courierName;

    /**
     * 快递员电话
     */
    @TableField("courier_phone")
    private String courierPhone;

    /**
     * 配送公司
     */
    @TableField("delivery_company")
    private String deliveryCompany;

    /**
     * 快递单号
     */
    @TableField("tracking_number")
    private String trackingNumber;

    /**
     * 预计送达时间
     */
    @TableField("estimated_time")
    private LocalDateTime estimatedTime;

    /**
     * 实际送达时间
     */
    @TableField("actual_time")
    private LocalDateTime actualTime;

    /**
     * 配送备注
     */
    @TableField("remark")
    private String remark;

    /**
     * 创建时间
     */
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;

    /**
     * 删除标记：0-未删除，1-已删除
     */
    @TableLogic
    @TableField("deleted")
    private Integer deleted;

    /**
     * 配送状态枚举
     */
    public enum DeliveryStatus {
        PENDING(1, "待配送"),
        IN_TRANSIT(2, "配送中"),
        DELIVERED(3, "已送达"),
        FAILED(4, "配送失败");

        private final Integer code;
        private final String desc;

        DeliveryStatus(Integer code, String desc) {
            this.code = code;
            this.desc = desc;
        }

        public Integer getCode() {
            return code;
        }

        public String getDesc() {
            return desc;
        }

        public static DeliveryStatus getByCode(Integer code) {
            for (DeliveryStatus status : values()) {
                if (status.getCode().equals(code)) {
                    return status;
                }
            }
            return null;
        }
    }
}